package com.example.tableapplication.dao;

import com.example.tableapplication.JavaBean.Content;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DbUtils {
    private String name = null;
    private String pass = null;

    public DbUtils(String m, String p){
        this.name = m;
        this.pass = p;
    }

    public DbUtils(){

    }



    private static Connection getSqlConnection(String ip, String user, String pwd, String db){
        Connection con =null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/"+ db +";charset=utf-8",user,pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException s){
            s.printStackTrace();
        }
        return con;
    }

    public List<Content> QureySql(){
        List<Content> result = new ArrayList<Content>();
        Connection conn = getSqlConnection("192.168.0.153","sa","renjie","rrr");
        String sql = " SELECT *  FROM  chengji ";
        try {
            PreparedStatement stat = conn.prepareStatement(sql);
//            stat.setString(1,"张三");
            ResultSet rs = stat.executeQuery();
            while (rs.next()){
                Content content = new Content(rs.getString("姓名"),rs.getString("语文"),rs.getString("数学"),rs.getString("英语"),rs.getString("物理"),rs.getString("化学"),rs.getString("生物"));
                result.add(content);
            }
            rs.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
//            result += "自定义查询数据库异常"+e.getMessage();
        }
        return result;
    }

}
